clear
discard
set more off

adopath ++ "./ado/"
adopath ++ "../tools/"
adopath ++ "../ssc/"

******************
*** Parameters ***
******************

local file_name "./input/consensus_dataset.csv"  // Data file
local output_folder "./output/state_dependence" // Output directory
local K = 4 // Number of lags to include when calculating local projections
local L = 4 // Number of IRF coefficients to estimate
local alpha = 0.05 // Significance level
local L_NW = 5 // Number of lags for calculating Newey-West standard errors

*************************************
*** Import data and set data type ***
*************************************

import delimited using `file_name', case(preserve)
rename DATE Date

gen qtr = quarterly(Date,"YQ")
format qtr %tq
tsset qtr // Set to time series

********************************
*** Generate forecast errors ***
********************************

gen Efor_Step2 = Realiz1 - SPFfor_Step2

*******************************************
*** Generate dummy variables for states ***
*******************************************

quietly summarize Realiz1, detail
scalar infl_p75 = r(p75)

gen abs_Efor_Step2 = abs(Efor_Step2)
quietly summarize abs_Efor_Step2, detail
scalar abs_fe_p75 = r(p75)

gen high_inflation = 0
replace high_inflation = 1 if Realiz1 >= infl_p75
replace high_inflation = . if missing(Realiz1)

gen large_fe  = 0
replace large_fe = 1 if abs_Efor_Step2 >= abs_fe_p75
replace large_fe = . if missing(abs_Efor_Step2)

gen great_moderation = 0
replace great_moderation = 1 if qtr >= quarterly("1985:01", "YQ") & ///
	qtr < quarterly("2007:01", "YQ")
replace great_moderation = . if missing(qtr)

****************************
*** Estimate regressions ***
****************************

* For actual inflation
estimate_IRF_LP Realiz1, k(`K') l(`L') alpha(`alpha') min_lags_NW(`L_NW') 
matrix IRF_actual_baseline = e(s)

estimate_IRF_LP_state_dep Realiz1 high_inflation , k(`K') l(`L') min_lags_NW(`L_NW')
matrix IRF_actual_high_inflation = e(s)

estimate_IRF_LP_state_dep Realiz1 NBER_recession , k(`K') l(`L') min_lags_NW(`L_NW')
matrix IRF_actual_NBER_recession = e(s)

estimate_IRF_LP_state_dep Realiz1 great_moderation , k(`K') l(`L') min_lags_NW(`L_NW')
matrix IRF_actual_great_moderation = e(s)

estimate_IRF_LP_state_dep Realiz1 large_fe , k(`K') l(`L') min_lags_NW(`L_NW')
matrix IRF_actual_large_fe = e(s) 

* For forecast errors
estimate_IRF_LP Efor_Step2, k(`K') l(`L') alpha(`alpha') min_lags_NW(`L_NW') 
matrix IRF_baseline = e(s)

estimate_IRF_LP_state_dep Efor_Step2 high_inflation , k(`K') l(`L') min_lags_NW(`L_NW')
matrix IRF_high_inflation = e(s)

estimate_IRF_LP_state_dep Efor_Step2 NBER_recession , k(`K') l(`L') min_lags_NW(`L_NW')
matrix IRF_NBER_recession = e(s)

estimate_IRF_LP_state_dep Efor_Step2 great_moderation , k(`K') l(`L') min_lags_NW(`L_NW')
matrix IRF_great_moderation = e(s)

estimate_IRF_LP_state_dep Efor_Step2 large_fe , k(`K') l(`L') min_lags_NW(`L_NW')
matrix IRF_large_fe = e(s)

* Clean up
shell rm -r `output_folder'
shell mkdir -p ./output
shell mkdir `output_folder'

* Save estimated IRFs
mat2txt, matrix(IRF_actual_baseline) saving(`output_folder'/IRF_actual_baseline.txt)
mat2txt, matrix(IRF_actual_high_inflation) saving(`output_folder'/IRF_actual_high_inflation.txt)
mat2txt, matrix(IRF_actual_NBER_recession) saving(`output_folder'/IRF_actual_NBER_recession.txt)
mat2txt, matrix(IRF_actual_great_moderation) saving(`output_folder'/IRF_actual_great_moderation.txt)
mat2txt, matrix(IRF_actual_large_fe) saving(`output_folder'/IRF_actual_large_fe.txt)

mat2txt, matrix(IRF_baseline) saving(`output_folder'/IRF_baseline.txt)
mat2txt, matrix(IRF_high_inflation) saving(`output_folder'/IRF_high_inflation.txt)
mat2txt, matrix(IRF_NBER_recession) saving(`output_folder'/IRF_NBER_recession.txt)
mat2txt, matrix(IRF_great_moderation) saving(`output_folder'/IRF_great_moderation.txt)
mat2txt, matrix(IRF_large_fe) saving(`output_folder'/IRF_large_fe.txt)
